package com.medical.doctor.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.medical.doctor.entity.DoctorMessageEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.Query;

import com.medical.doctor.dao.DoctorLabelDao;
import com.medical.doctor.entity.DoctorLabelEntity;
import com.medical.doctor.service.DoctorLabelService;


@Service("doctorLabelService")
public class DoctorLabelServiceImpl extends ServiceImpl<DoctorLabelDao, DoctorLabelEntity> implements DoctorLabelService {

    @Autowired
    private DoctorLabelDao doctorLabelDao;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DoctorLabelEntity> page = this.page(
                new Query<DoctorLabelEntity>().getPage(params),
                new QueryWrapper<DoctorLabelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<DoctorLabelEntity> queryPageByName(DoctorLabelEntity doctorLabelEntity) {

        String label = redisTemplate.opsForValue().get("Label");
        //
        if (label!=null){
            System.out.println("从缓存中拿");
            //转成list集合
            List<DoctorLabelEntity> doctorLabelEntities = JSON.parseObject(label, new TypeReference<List<DoctorLabelEntity>>() {
            });
            return doctorLabelEntities;
        }else {
            System.out.println("从数据库拿");

            List<DoctorLabelEntity> doctorLabelEntities = doctorLabelDao.queryPageByName(doctorLabelEntity);

            String s = JSON.toJSONString(doctorLabelEntities);

            //设置一天的过期时间
            redisTemplate.opsForValue().set("Label",s,1440,TimeUnit.MINUTES);
            return doctorLabelEntities;
        }




    }


}